<?php

namespace App\Http\Controllers;

use App\Models\Floor;
use App\Models\Device;
use Illuminate\Http\Request;

class DeviceController extends Controller
{

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deviceTypeList(Request $request)
    {

        $devices = Device::select(['type'])->where('type', '!=', 1)->groupby('type')->get();
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $devices
        ];
        return Response()->json($responseJson);
    }

    /**
     * @return void
     */
    public function deviceList(Request $request)
    {
        $validatedData = $request->validate([
            'page' => 'nullable|integer',
            'pageSize' => 'nullable|integer',
        ]);
        $pageSize = $validatedData['pageSize'] ?? 20; // 默认每页20条记录
        $devices = Device::with(['floor', 'room', 'device485'])->where(['type' => 1])->paginate($pageSize);
        $data = [
            'page' => $devices->currentPage(),
            'pageSize' => intval($devices->perPage()),
            'rows' => $devices->items(),
            'total' => $devices->total(),
        ];
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $data
        ];
        return Response()->json($responseJson);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function iotList(Request $request)
    {
        $validatedData = $request->validate([
            'page' => 'nullable|integer',
            'pageSize' => 'nullable|integer',
            'type' => 'nullable',
            'floor_id' => 'nullable',
            'room_id' => 'nullable'
        ]);
        $pageSize = $validatedData['pageSize'] ?? 20; // 默认每页20条记录
        $query = Device::with(['floor', 'room', 'device485'])->where('type', '!=', 1);
        if (!empty($validatedData['type'])) {
            $query->where(['type' => $validatedData['type']]);
        }
        if (!empty($validatedData['floor_id'])) {
            $query->where(['floor_id' => $validatedData['floor_id']]);
        }
        if (!empty($validatedData['room_id'])) {
            $query->where(['room_id' => $validatedData['room_id']]);
        }
        $devices = $query->paginate($pageSize);
        $data = [
            'page' => $devices->currentPage(),
            'pageSize' => intval($devices->perPage()),
            'rows' => $devices->items(),
            'total' => $devices->total(),
        ];
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $data
        ];
        return Response()->json($responseJson);
    }

    /**
     * @return void
     */
    public function deviceAdd(Request $request)
    {
        $val = $request->validate(
            [
                "id" => 'nullable',
                'name' => 'required',
                'floor_id' => 'nullable',
                'room_id' => 'nullable',
                'account' => 'nullable',
                'type' => 'nullable',
            ]
        );
        if (!empty($val['id'])) { // 更新逻辑
            $data = Device::find($val['id']);
            $data->fill($val);
            $data->save();
        } else {
            $data = Device::create($val);
        }
        $responseJson = [
            'code' => 200,
            'message' => '',
            'data' => $data->id
        ];
        return Response()->json($responseJson);
    }

    /**
     * @return void
     */
    public function deviceDel($ids)
    {
        if (strpos($ids, ',') !== false) {
            $numbers = explode(',', $ids);
            foreach ($numbers as $num) {
                $device = Device::findOrFail($num);
                $device->delete();
            }
        } else {
            $device = Device::findOrFail($ids);
            $device->delete();
        }
        $responseJson = [
            'code' => 200,
            'message' => '成功删除',
        ];
        return response()->json($responseJson);
    }
}
